var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require("webpack");
var HtmlWebpackPlugin = require('html-webpack-plugin');
//环境变量的配置dev/online
var WEBPACK_ENV = process.env.WEBPACK_ENV ||"dev";

//获取html-webpack-plugin参数方法
var getHtmlConfig = function(name,title,directory){
    if(directory){
        return{
            template : "./src/view/"+directory+"/"+name+".html",
            filename : "view/"+name+".html",
            favicon: './favicon.ico',
            title : title,
            inject : true,
            hash:true,
            chunks:["common",name]
        }
    }else{
        return{
            template : "./src/view/"+name+".html",
            filename : "view/"+name+".html",
            favicon: './favicon.ico',
            title : title,
            inject : true,
            hash:true,
            chunks:["common",name]
        }
    }

}
var config = {
    //1.入口
    entry:{
        "common":["./src/page/common/common.js"],
        "index" :["./src/page/index/index.js"],
        "user-login" :["./src/page/user-login/user-login.js"],
        "user-register" :["./src/page/user-register/user-register.js"],
        "user-password-reset" :["./src/page/user-password-reset/user-password-reset.js"],
        "user-center" :["./src/page/user-center/user-center.js"],
        "user-center-update" :["./src/page/user-center-update/user-center-update.js"],
        "user-password-update" :["./src/page/user-password-update/user-password-update.js"],
        "user-img-center" :["./src/page/user-img-center/user-img-center.js"],
        "seller-register" :["./src/page/seller/seller-register/seller-register.js"],
        "seller-product-save" :["./src/page/seller/seller-product-save/seller-product-save.js"],
        "seller-product-list" :["./src/page/seller/seller-product-list/seller-product-list.js"],
        "seller-product-detail" :["./src/page/seller/seller-product-detail/seller-product-detail.js"],
        "seller-order-list" :["./src/page/seller/seller-order-list/seller-order-list.js"],
        "seller-order-detail" :["./src/page/seller/seller-order-detail/seller-order-detail.js"],

        "product-list" :["./src/page/product-list/product-list.js"],
        "product-detail" :["./src/page/product-detail/product-detail.js"],
        "cart" :["./src/page/cart/cart.js"],
        "order-confirm" :["./src/page/order-confirm/order-confirm.js"],
        "order-list" :["./src/page/order-list/order-list.js"],
        "order-detail" :["./src/page/order-detail/order-detail.js"],
        "payment" :["./src/page/payment/payment.js"],
        "result" :["./src/page/result/result.js"],/*结果提示*/
        "test" :["./src/page/test/test.js"],
        "test2" :["./src/page/test/test2.js"]
    },
    //2.输出文件
    output:{
        path: __dirname+"/dist",
        publicPath: "dev"===WEBPACK_ENV ? "/dist/" : "//s.choisaaaa.shop/CampusO2OUserView/dist/",
        filename:"js/[name].js"
    },
    //3.把外部的模块、变量加载进来
    externals:{
        "jquery" :"window.jQuery"
    },
    //4.加载的模块
    module:{
        loaders:[
            //对css文件的处理
            {test:/\.css$/,loader:ExtractTextPlugin.extract("style-loader","css-loader","postcss-loader","less-loader")},
            //对图片的处理
            { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=1&name=resource/[name].[ext]'},
            //对.string文件的处理
            {
                test: /\.string$/,
                loader: 'html-loader',
                query : {
                    minimize : true,
                    removeAttributeQuotes : false//不删除引号
                }
            },
        ]
    },
    //5.给文件取别名，方便路径访问
    resolve:{
        alias : {
            node_modules : __dirname + "/node_modules",//__diename(两个横杆)表示当面目录
            util : __dirname + '/src/util',
            page : __dirname + '/src/page',
            service  : __dirname + '/src/service',
            image  : __dirname + '/src/image'
        }
    },
    //6.插件
    plugins:[
        //通用模块打包到js/base.js
        new webpack.optimize.CommonsChunkPlugin({
            name :"common",
            filename:"js/base.js"
        }),
        //css文件单独打包
        new ExtractTextPlugin('css/[name].css'),
        //html模块的处理
        new HtmlWebpackPlugin(getHtmlConfig("index","CampusO2O_让爱漂流")),
        new HtmlWebpackPlugin(getHtmlConfig("user-login","用户登录")),
        new HtmlWebpackPlugin(getHtmlConfig("user-register","用户注册")),
        new HtmlWebpackPlugin(getHtmlConfig("user-center","我的个人中心")),
        new HtmlWebpackPlugin(getHtmlConfig("user-center-update","更新个人信息")),
        new HtmlWebpackPlugin(getHtmlConfig("user-password-reset","我要找回密码")),
        new HtmlWebpackPlugin(getHtmlConfig("user-password-update","更新密码")),
        new HtmlWebpackPlugin(getHtmlConfig("user-img-center","我的头像")),
        new HtmlWebpackPlugin(getHtmlConfig("seller-register","店家注册","seller")),
        new HtmlWebpackPlugin(getHtmlConfig("seller-product-save","发布商品","seller")),
        new HtmlWebpackPlugin(getHtmlConfig("seller-product-list","我的商品","seller")),
        new HtmlWebpackPlugin(getHtmlConfig("seller-product-detail","商品详情","seller")),
        new HtmlWebpackPlugin(getHtmlConfig("seller-order-list","接收订单列表","seller")),
        new HtmlWebpackPlugin(getHtmlConfig("seller-order-detail","接收订单详情","seller")),
        new HtmlWebpackPlugin(getHtmlConfig("product-list","商品列表")),
        new HtmlWebpackPlugin(getHtmlConfig("product-detail","商品详情")),
        new HtmlWebpackPlugin(getHtmlConfig("cart","我的购物车")),
        new HtmlWebpackPlugin(getHtmlConfig("order-confirm","确认订单")),
        new HtmlWebpackPlugin(getHtmlConfig("order-list","订单列表")),
        new HtmlWebpackPlugin(getHtmlConfig("order-detail","订单详情")),
        new HtmlWebpackPlugin(getHtmlConfig("payment","订单支付")),
        new HtmlWebpackPlugin(getHtmlConfig("result","操作结果")),
        new HtmlWebpackPlugin(getHtmlConfig("test","测试")),
        new HtmlWebpackPlugin(getHtmlConfig("test2","测试"))


    ],

    //7.配置代理
    devServer: {
        port: 8089,
        inline: true,
        proxy : {
            //pro环境中使用下面的配置
            // '/image/richtext_img_upload.do' : {
            //     target: 'http://www.choisaaaa.campuso2o.com/',
            //     changeOrigin : true
            // }

            // dev开发使用下面的配置
              '**/*.do' : {
                // 这个域名是开发时配置的域名，其实是通过修改host文件，让www.dev.campuso2o.com访问请求到本地
                // 再通过nginx将这个请求代理到本地的不同tomcat（分布式部署）中
                // 注：如果是但实例部署，可以配置 ： target: 'http://localhost:8088/campusO2O（本地后台项目的访问路径）',
                target: 'http://www.dev.campuso2o.com/campuso2o/',
                changeOrigin : true

            }
        }
    }
}
//开发环境
if("dev"===WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8089/');
}
module .exports = config;


